<?php

namespace app\admin\controller;

use app\common\model\Classes;
use app\common\model\ClassesXueyuan;
use app\common\model\XueyuanFankui;
use app\common\model\Xueyuan;

class Fankui extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 分页获取信息
     * @return void
     */
    public function list()
    {
        //查询条件
        $where = array();

        if (input('name')) {
            $where['name'] = array('like', "%" . input('name') . "%");
        }
        if (input('manager_name')) {
            $where['manager_name'] = array('eq', input('manager_name'));
        }

        //分页信息
        $page = max(input('page'), 1);
        $size = 20;
        //获取分页列表
        $list = XueyuanFankui::getPage($where, $page, $size);
        //总页码
        $total = XueyuanFankui::getCount($where);
        //数据返回
        if (!$list) {
            return returnJson(0, '', array('list' => [], 'total' => 0));
        }
        return returnJson(0, '', [
            'list' => $list,
            'size' => $size,
            'total' => $total
        ]);
    }

    public function weifankuiList()
    {
        //查询条件
        $where = [
            'status'=>1
        ];
        if ($mid = input('mid')) {
            $cids = Classes::getColumn(array('mid' => $mid), 'id');
            if ($cids)
                $sids = ClassesXueyuan::getColumn(array('cid' => ['in', $cids]), 'sid');
            if (isset($sids) && $sids)
                $where['id'][] = ['in', $sids];
        }

        if ($date = input('date')) {
            $date = $date . '-1';
            $date = date('Y-m',strtotime($date));
            $sids = XueyuanFankui::getColumn(array('date'=>$date),'sid');
            $where['id'][] = ['not in',$sids];
        }

        $list = Xueyuan::getAll($where,'id,name');

        return returnJson(0, '',$list?$list:[]);

    }


    /**
     * 新增或修改班级
     * @return \think\response\Json|void
     */
    public function edit()
    {
        if (request()->isPost()) {
            $input = input();
            $time = time();
            $input['date'] = date('Y-m',strtotime($input['date'].'-1'));
            $input['update_time'] = $time;
            $input['create_time'] = $time;
            $res = XueyuanFankui::setInsertGetId($input);
            if ($res) {
                return returnJson(0, '操作成功');
            }
            return returnJson(1, '网络异常，请重试');
        }
    }

    /**
     * 反馈删除
     * @return void
     */
    public function delete()
    {
        // 查询条件
        $where = [];

        if (input('id')) {
            $where['id'] = array('eq', input('id'));
        }

        XueyuanFankui::setDel($where);

        //返回信息
        return returnJson(0, '');
    }



}
